package com.ly.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.system.comm.util.Snowflake;
import com.system.common.mapper.BisShopMapper;
import com.system.common.mapper.BisShopUserMapper;
import com.system.common.pojo.BisShop;
import com.system.common.pojo.BisShopExample;
import com.system.common.pojo.BisShopExample.Criteria;
import com.system.common.pojo.BisShopUser;
import com.system.common.pojo.BisShopUserExample;



@Service
public class ShopServiceImpl implements ShopService{
	@Autowired
	private BisShopMapper shopMapper;
	@Autowired
	private BisShopUserMapper shopusermapper;
	
	/**
	 * 查询所有的店铺信息
	 */

	@Override	
	public List<BisShop> listAll(Map<?,?> map) {
		String sid=(String) map.get("sid");
		String sname=(String) map.get("sname");
		String showStatus=(String) map.get("showStatus");
		//String account=(String) map.get("account");
		String sjname=(String) map.get("sjname");
		String lid=(String) map.get("lid");
		BisShopExample example=new BisShopExample();
		Criteria criteria=example.createCriteria();
	
	if (StringUtils.isNotBlank(sid)) {
		criteria.andShopIdLike("%"+sid+"%");
	}
	
	if (StringUtils.isNotBlank(sname)) {
		sname="%"+sname+"%";
	}
	if (StringUtils.isNotBlank(sname)) {
		criteria.andShopNameLike(sname);
	}	
	
	if (StringUtils.isNotBlank(showStatus)) {
		criteria.andShowStatusEqualTo(showStatus);
	}
	
	if (StringUtils.isNotBlank(sjname)) {
		sjname="%"+sjname+"%";
	}
	if (StringUtils.isNotBlank(sjname)) {
		criteria.andShortNameLike(sjname);
	}
	
	
	if (StringUtils.isNotBlank(lid)) {
		lid="%"+lid+"%";
	}
	if (StringUtils.isNotBlank(lid)) {
		criteria.andShopLicenseLike(lid);
	}		
		
		
		List<BisShop> shops=new ArrayList<>();
		List<BisShop> list=shopMapper.listAll(map);
		for (BisShop shop : list) {
			if (shop.getIsDel().equals("0")) {
				shops.add(shop);
			}
		}
		
		return shops;
	}

	/**
	 * 查询所有的店长信息，用于添加店铺信息
	 */
	@Override
	public List<BisShopUser> queryShopUserAccount() {
	 BisShopUserExample example=new BisShopUserExample();
	 com.system.common.pojo.BisShopUserExample.Criteria criteria=example.createCriteria();
		criteria.andUserTypeEqualTo("1");
		List<BisShopUser> shopuseraccountlist=shopusermapper.selectByExample(example);
	 return shopuseraccountlist;
	}
	
/**
 * 添加
 */
	@Override
	public void insert(BisShop shop) {
		Snowflake sf=new Snowflake(0, 0);
		shop.setShopId(sf.nextId()+"");
		shop.setIsDel("0");
		shop.setModifyTime(new Date());
		this.shopMapper.insert(shop);
	}


	@Override//根据用户id查询该用户所在的所有店铺信息
	public List<BisShop> queryByShopUserId(String ownerId) {
		BisShopExample example=new BisShopExample();
		//初始化一个查询条件
		Criteria criteria=example.createCriteria();
		if (StringUtils.isNotBlank(ownerId)) {
			criteria.andOwnerIdEqualTo(ownerId);
		}
		List<BisShop> shoplist=shopMapper.selectByExample(example);
		return shoplist;
	}

	@Override//根据主键查询
	public BisShop queryShopByShopId(String shopId) {
		BisShop shop=shopMapper.selectByPrimaryKey(shopId);
		return shop;
	}

	@Override//修改
	public void update(BisShop shop) {
		shopMapper.updateByPrimaryKeySelective(shop);
	}

	@Override//删除
	public void delete(String shopId) {
		BisShop shop=shopMapper.selectByPrimaryKey(shopId);
		shop.setIsDel("1");
		shopMapper.updateByPrimaryKey(shop);
	}


	


	
	

}
